using System.Collections;
using System.Collections.Generic;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine.AddressableAssets;
using UnityEngine;
using System;
using System.Linq;
using Object = System.Object;

public static class LoadSystem
{
    public static T LoadAsset<T>(string path) where T : ScriptableObject
    {
        if (SFrameworkGate.InEditor)
        {

#if DEBUG && UNITY_EDITOR
            return AssetDatabase.LoadAssetAtPath<T>(path);
#endif
        }
        return default(T);
    }
    public static Dictionary<string, AssetUnit> table = new();
    public static void LoadAllAssets()
    {
        Addressables.LoadResourceLocationsAsync("default").Completed += (e) =>
        {
            if (!e.IsValid()) return;
            foreach (var location in e.Result)
            {
                Addressables.LoadAssetAsync<System.Object>(location.PrimaryKey).Completed += (hanlder) =>
                {
                    if (!hanlder.IsValid()) return;

                    table[location.PrimaryKey] = new AssetUnit()
                    {
                        obj = hanlder.Result,
                        assetType = AssetTypeCast[location.ResourceType]
                    };
                };
            }
        };
    }
    public static Dictionary<Type, LoadAssetType> AssetTypeCast = new Dictionary<Type, LoadAssetType>()
    {
        [typeof(AnimationClip)] = LoadAssetType.AnimationClip,
        [typeof(AudioClip)] = LoadAssetType.AudioClip,
    };

    public struct AssetUnit
    {
        public object obj;
        public LoadAssetType assetType;
    }
#if UNITY_EDITOR
    /// <summary>
    /// 从指定文件夹下的 FBX 文件中提取第一个 AnimationClip
    /// </summary>
    /// <param name="folderPath">例如："Animations/FBX"</param>
    /// <param name="fbxName">例如："Run.fbx"</param>
    /// <returns>提取到的第一个 AnimationClip，如果没有则返回 null</returns>
    public static AnimationClip LoadClipEditor(string folderPath, string fbxName)
    {
        // 拼接完整路径
        string fullAssetPath = Path.Combine("Assets", folderPath, fbxName).Replace("\\", "/");
        if (!(fullAssetPath.EndsWith(".fbx") || fullAssetPath.EndsWith(".FBX")))
        {
            fullAssetPath += ".fbx";
        }

        // 检查文件是否存在
        if (!File.Exists(fullAssetPath))
        {
            Debug.LogError($"找不到 FBX 文件: {fullAssetPath}");
            return null;
        }

        // 加载所有子资源
        Object[] assets = AssetDatabase.LoadAllAssetsAtPath(fullAssetPath);
        foreach (Object obj in assets)
        {
            if (obj is AnimationClip clip && !clip.name.Contains("__preview__"))
            {
                return clip;
            }
        }

        Debug.LogWarning($"FBX 文件中没有有效的 AnimationClip: {fbxName}");
        return null;
    }
    public static List<string> CollectName(string path)
    {
        List<string> files = new List<string>();

        string fullPath = Path.Combine(Application.dataPath, path);
        if (!Directory.Exists(fullPath))
        {
            Debug.LogWarning($"路径不存在: {fullPath}");
            return files;
        }

        string[] allFiles = Directory.GetFiles(fullPath, "*.*", SearchOption.AllDirectories);

        foreach (string file in allFiles)
        {
            if (file.EndsWith(".meta")) continue; // 忽略.meta文件

            // 转换为 Unity 的 Asset 路径
            string assetPath = file.Replace(Application.dataPath, "").Replace("\\", "/").Replace(path, "");

            files.Add(assetPath);
        }

        return files;
    }
#endif

    public static AssetUnit ToAsset(this string assetName)
    {
        return table[assetName];
    }
}


//这种就要求资产在命名中就考虑到编程，从而使得资源id全局唯一
//这里简单的根据Demo写一个资产加载
public enum LoadAssetType
{
    AnimationClip = 0,
    AudioClip = 1,
    Effect = 2,
}